package com.example.scenictourback.service.impl;

import com.example.scenictourback.dto.ScenicSpotDTO;
import com.example.scenictourback.entity.ScenicArea;
import com.example.scenictourback.entity.ScenicSpot;
import com.example.scenictourback.repository.ScenicAreaRepository;
import com.example.scenictourback.repository.ScenicSpotRepository;
import com.example.scenictourback.service.ScenicSpotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityNotFoundException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScenicSpotServiceImpl implements ScenicSpotService {

    @Autowired
    private ScenicSpotRepository scenicSpotRepository;
    
    @Autowired
    private ScenicAreaRepository scenicAreaRepository;

    @Override
    public List<ScenicSpotDTO> listScenicSpotsByScenicAreaId(Integer scenicAreaId) {
        List<ScenicSpot> scenicSpots = scenicSpotRepository.findByScenicAreaId(scenicAreaId);
        return scenicSpots.stream()
                .map(ScenicSpotDTO::fromEntity)
                .collect(Collectors.toList());
    }

    @Override
    public ScenicSpotDTO getScenicSpotById(Integer id) {
        ScenicSpot scenicSpot = scenicSpotRepository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException("景点不存在"));
        return ScenicSpotDTO.fromEntity(scenicSpot);
    }

    @Override
    public List<ScenicSpotDTO> searchScenicSpotsByName(String name) {
        List<ScenicSpot> scenicSpots = scenicSpotRepository.findByNameContaining(name);
        return scenicSpots.stream()
                .map(ScenicSpotDTO::fromEntity)
                .collect(Collectors.toList());
    }

    @Override
    public ScenicSpotDTO addScenicSpot(ScenicSpot scenicSpot) {
        // 设置所属景区
        ScenicArea scenicArea = scenicAreaRepository.findById(scenicSpot.getScenicArea().getId())
                .orElseThrow(() -> new EntityNotFoundException("景区不存在"));
        scenicSpot.setScenicArea(scenicArea);
        
        // 保存景点
        ScenicSpot savedScenicSpot = scenicSpotRepository.save(scenicSpot);
        return ScenicSpotDTO.fromEntity(savedScenicSpot);
    }

    @Override
    public ScenicSpotDTO updateScenicSpot(Integer id, ScenicSpot scenicSpot) {
        // 检查景点是否存在
        ScenicSpot existingScenicSpot = scenicSpotRepository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException("景点不存在"));
        
        // 设置所属景区
        ScenicArea scenicArea = scenicAreaRepository.findById(scenicSpot.getScenicArea().getId())
                .orElseThrow(() -> new EntityNotFoundException("景区不存在"));
        
        // 更新景点信息
        existingScenicSpot.setScenicArea(scenicArea);
        existingScenicSpot.setName(scenicSpot.getName());
        existingScenicSpot.setDescription(scenicSpot.getDescription());
        
        // 保存更新
        ScenicSpot updatedScenicSpot = scenicSpotRepository.save(existingScenicSpot);
        return ScenicSpotDTO.fromEntity(updatedScenicSpot);
    }

    @Override
    public void deleteScenicSpot(Integer id) {
        // 检查景点是否存在
        if (!scenicSpotRepository.existsById(id)) {
            throw new EntityNotFoundException("景点不存在");
        }
        
        // 删除景点
        scenicSpotRepository.deleteById(id);
    }
} 